package com.link.base.base.appinfo.controller;

import com.link.base.base.appinfo.model.AppManager;
import com.link.base.base.appinfo.service.AppManagerService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.controller.BasicControllerException;
import com.link.core.basic.service.BasicService;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 移动应用管理
 *
 * @author ouyangzhenqiang
 * @date 2018/11/15
 */
@Controller
@RequestMapping("/link/appManager")
public class AppManagerController extends BasicController<AppManager> {

    @Resource
    private AppManagerService appManagerService;

    @Override
    public BasicService<AppManager> getBasicService() throws Exception {
        return appManagerService;
    }

    /**
     * 应用发布
     *
     * @author ouyangzhenqiang
     * @date 2018/11/17
     */
    @PostMapping(value = "/publishApp", name = "移动应用发布")
    @ResponseBody
    public Map<String, Object> publishApp(@JsonParam AppManager record, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            appManagerService.publishApp(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 应用下架
     *
     * @author ouyangzhenqiang
     * @date 2018/11/17
     */
    @PostMapping(value = "/soldOutApp", name = "移动应用下架")
    @ResponseBody
    public Map<String, Object> soldOutApp(@JsonParam AppManager record, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            appManagerService.soldOutApp(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * 获取应用列表，包括应用下最新版本记录信息
     *
     * @author ouyangzhenqiang
     * @date 2018/11/18
     */
    @PostMapping(value = "/getAppList", name = "移动应用列表")
    @ResponseBody
    public Map<String, Object> getAppList(@JsonParam AppManager record, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            List<AppManager> list = appManagerService.getAppList(record);
            result.put("success", true);
            result.put("result", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 根据Id获取应用信息和最新版本信息
     *
     * @author ouyangzhenqiang
     * @date 2018/11/18/link/guide/
     */
    @GetMapping(value = "/getAppById", name = "移动应用基本信息")
    @ResponseBody
    public Map<String, Object> getAppById(AppManager record, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            if (null == record.getId()) {
                throw new BasicControllerException("Id不能为空.");
            }
            AppManager temp = appManagerService.getAppById(record);
            result.put("success", true);
            result.put("result", temp);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
